<?php

namespace app\api\model;

use think\Model;
/**
 * 互动讨论
 */
class InteractModel extends Model
{
    // 表名
    protected $name = 'interact';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;

    // 追加属性
    protected $append = [
        'time_describe',
        'image_arr',
    ];

    public function getTimeDescribeAttr($value, $data)
    {
        $time = $data['createtime'] ? tranTime($data['createtime']) : '';
        return $time;
    }
    public function getImageArrAttr($value, $data)
    {
        $arr = $data['images'] ? explode(',',$data['images']) : [];
        return $arr;
    }


    public function theme()
    {
        return $this->hasOne('InteractThemeModel', 'id', 'theme_id')->field('id,title');
    }

    public function comment()
    {
        // 取出最新的两条评论
        $where = [
            'style' => 'Interact',
            'top_id' => '0',
        ];
        return $this->hasMany('CommentModel', 'relation_id', 'id')->where($where)->field('id,relation_id,reply_type,reply_id,content,createtime')->limit(2)->order('createtime desc');
    }


}
